package ch.ffhs.esma.bunk_weiss.notepad;

/**
 * @author: Julian Bunk
 * @Date: 04.01.2015
 */

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class ShowNotes extends Activity implements OnClickListener {

	private TextView titleview = null;
	private TextView noticetextview = null;
	private TextView dateview = null;
	private TextView timeview = null;
	String title = null;
	String noticetext = null;
	String date = null;
	String time = null;
	String bildpfad = null;
	String soundpfad = null;
	Bitmap bildBitmap;
	ImageButton myImage;
	ImageButton mySound;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.show_notes_activity);

		Bundle bundle = getIntent().getExtras();

		// Get Title from ListView
		title = bundle.getString("title");
		titleview = (TextView) findViewById(R.id.outputTextTitel);
		titleview.setText(title);

		// Get Noticetext from ListView
		noticetext = bundle.getString("noticetext");
		noticetextview = (TextView) findViewById(R.id.outputTextNote);
		noticetextview.setText(noticetext);

		// Get Date from ListView
		date = bundle.getString("date");
		dateview = (TextView) findViewById(R.id.outputTextDate);
		dateview.setText(date);

		// Get Time from ListView
		time = bundle.getString("time");
		timeview = (TextView) findViewById(R.id.outputTextTime);
		timeview.setText(time);

		// Get AudiorecordFile from Directory
		soundpfad = bundle.getString("soundpfad");// String Sound-File
		mySound = (ImageButton) findViewById(R.id.imageButton1);
		mySound.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				MediaPlayer mp = new MediaPlayer();
				try {
					mp.setDataSource(soundpfad);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					mp.prepare();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mp.start();
			}
		});

		// Get Picture from Directory
		bildpfad = bundle.getString("bildpfad");// String Bild-File
		final File bildBitmap = new File(bildpfad);
		myImage = (ImageButton) findViewById(R.id.imageButton2);
		myImage.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				Uri path = Uri.fromFile(bildBitmap);
				Intent imageOpenintent = new Intent(Intent.ACTION_VIEW);
				imageOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				imageOpenintent.setDataAndType(path, "image/jpg");
				try {
					startActivity(imageOpenintent);
				} catch (ActivityNotFoundException e) {
				}
			}
		});

		// Back to Main-Menu
		View verwerfen = findViewById(R.id.backtomain);
		verwerfen.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		if (arg0.getId() == R.id.backtomain) {
			// define a new Intent for the Home Activity
			Intent intent = new Intent(this, Home.class);

			// start the Home Activity
			this.startActivity(intent);
			finish();
		}
	}
}